//
//  RecommandViewController.m
//  Travel
//
//  Created by lanou on 15/9/18.
//  Copyright (c) 2015年 TeamThree. All rights reserved.
//

#import "RecommandViewController.h"
#import "RecommendScrollView.h"
#import "WonderfulStroyCollectionViewCell.h"
#import "RecommendCollectionReusableView.h"
#import "SpecialTopicCollectionViewCell.h"
#import "MoreStoryViewController.h"

@interface RecommandViewController ()<UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
// 创建collectionView
@property (nonatomic, retain) UICollectionView *collectionView;

// 设置轮播图
@property (nonatomic, retain) RecommendScrollView *scrollView;

// 设置pagecontol
@property (nonatomic, retain) UIPageControl *pageControl;

// 存放轮播图
@property (nonatomic, retain) NSMutableArray *imageArr;

@end

@implementation RecommandViewController

- (void)dealloc {
    
    [_collectionView release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpRecommandView];
    [self setUpPageControl];
    [self reloadDataWithEveryday];
}

- (void)reloadDataWithEveryday {
    [LORequestManger GET:kEverydayUrl success:^(id response) {
        
//        NSDictionary *dic = (NSDictionary *)response;
//        for (NSDictionary  in <#collection#>) {
//            <#statements#>
//        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}


#pragma mark --------------- 懒加载 ----------------
//- (NSMutableArray *)imageArr {
//    if (!_imageArr) {
//        _imageArr = [NSMutableArray array];
//    }
//    return _imageArr;
//}

#pragma mark --------------- 设置推荐页面 ----------------

- (void)setUpRecommandView {

    // 设置collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:kBounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor colorWithRed:0.969 green:0.949 blue:0.902 alpha:1.000];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[WonderfulStroyCollectionViewCell class] forCellWithReuseIdentifier:@"WonderfulCell"];
    [_collectionView registerClass:[RecommendCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommend"];
    [_collectionView registerClass:[SpecialTopicCollectionViewCell class] forCellWithReuseIdentifier:@"SpecialTop"];
    
    // 设置轮播图
    self.imageArr = [NSMutableArray arrayWithObjects:@"努力.jpg", @"坚持.jpg", @"小老鼠.jpg", @"方向.jpg", @"风景.jpg", nil];
    _scrollView = [[RecommendScrollView alloc] initWithFrame:(CGRectMake(0, -200, kWidth, kHeight / (667.0 / 200))) imageArr:self.imageArr delegate:self action:@selector(actionTapImage) timer:3.5 selector:@selector(cirulrly)];
    [_collectionView addSubview:_scrollView];
    
    [layout release];
}

- (void)setUpPageControl {
    _pageControl = [[UIPageControl alloc] initWithFrame:(CGRectMake(0, -35, kWidth, 30))];
    _pageControl.numberOfPages = self.imageArr.count;
    _pageControl.currentPage = 0;
    [_collectionView addSubview:_pageControl];
}

#pragma mark --------------- 轮播图的点击事件 ----------------

- (void)actionTapImage {
    
//    NSInteger index = _scrollView.contentOffset.x / kWidth;
//    index -= 1;
//    if (index == 5) {
//        index = 0;
//    }
}


#pragma mark --------------- 定时器添加事件 ----------------

- (void)cirulrly{
    [self setUpScrollViewWithCirulrly];
    CGPoint newPoint = _scrollView.contentOffset;
    newPoint.x += kWidth;
    _pageControl.currentPage += 1;
    if (_pageControl.currentPage == 5) {
        _pageControl.currentPage = 0;
    }
    [_scrollView setContentOffset:newPoint animated:YES];
    
}

// 设置轮播图效果
- (void)setUpScrollViewWithCirulrly {
    if (_scrollView.contentOffset.x == kWidth * (self.imageArr.count + 1)) {
        _pageControl.currentPage = 0;
        [_scrollView setContentOffset:(CGPointMake(kWidth, 0)) animated:NO];
    } else if (_scrollView.contentOffset.x == 0) {
        [_scrollView setContentOffset:(CGPointMake(kWidth * self.imageArr.count, 0)) animated:YES];
        _pageControl.currentPage = 4;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self cirulrly];
}

#pragma mark --------------- collectionView代理 ----------------

// 返回分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
// 返回分区的行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    } else {
        return 10;
    }
}
// 设置collectionView的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        WonderfulStroyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WonderfulCell" forIndexPath:indexPath];
        return cell;
    } else {
        SpecialTopicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SpecialTop" forIndexPath:indexPath];

        return cell;
    }

}
// 设置增广视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    RecommendCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommend" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        reusableView.titleLabel.text = @"每日精选故事";
        reusableView.moreLabel.text = @"全部";
        [reusableView.arrowBtn addTarget:self action:@selector(clickMoreStory) forControlEvents:(UIControlEventTouchUpInside)];
    } else if (indexPath.section == 1){
        reusableView.titleLabel.text = @"精彩游记和专题";
        reusableView.moreLabel.text = @" ";
    }
    
    return reusableView;
    
}

// 更多故事按钮点击
- (void)clickMoreStory {
    MoreStoryViewController *moreStoryVC = [[MoreStoryViewController alloc] init];
    [self.navigationController pushViewController:moreStoryVC animated:YES];
    [moreStoryVC release];
}

//设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
   if (section == 1){
        return 10;
    } else {
        return 13;
    }
}

// 设置每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake((kWidth - 30) / 2, kHeight / (677.0 / 220));
    } else {
        return CGSizeMake(kWidth - 20, kHeight / (677.0 / 215));
    }
}

// 设置距上 下 左 右的间隔
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 0, 10);
}
// 设置表头的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake(kWidth, 55);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
